package com.itlong.cloud.controller.finance;

import com.itlong.cloud.POJO.DTO.property.PropertyAmountChangeGetPageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyAmountChangeGetPageVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.paymenthub.IPropertyAmountChangeClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <desc>
 *      物业-资金变动明细控制器
 * </desc>
 *
 * @createDate 2018/09/25
 */
@RestController
@RequestMapping("/finance/propertyAmountChange")
public class PropertyAmountChangeController extends BaseController {

    @Autowired
    private IPropertyAmountChangeClient iPropertyAmountChangeClient;

    /**
     * <desc>
     *      分页获取资金变动明细
     * </desc>
     *
     * @param propertyAmountChangeGetPageDTO 物业-资金变动明细列表查询DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2018/09/25
     */
    @ResponseBody
    @PostMapping("getAmountChangeByPage")
    public Object getAmountChangeByPage(PropertyAmountChangeGetPageDTO propertyAmountChangeGetPageDTO){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyAmountChangeGetPageDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyAmountChangeGetPageDTO.class, request);
            if (null == propertyAmountChangeGetPageDTO ) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isBlank(propertyAmountChangeGetPageDTO.getProjectId())) {
                return new MessageVO(PropertyErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }
            if (StringUtils.isBlank(propertyAmountChangeGetPageDTO.getTime())) {
                propertyAmountChangeGetPageDTO.setTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            }
            Page<PropertyAmountChangeGetPageVO> page = iPropertyAmountChangeClient.getAmountChangeByPage(propertyAmountChangeGetPageDTO);
            if (page.getRows() == null || page.getRows().isEmpty()) {
                return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),(Object) new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        } catch (Exception e) {
            throw new DataAccessException("【物业-资金变动明细getAmountChangeByPage】分页获取物业资金变动明细信息失败",e);
        }
    }

}
